package v4

import (
	"fmt"
	"log"
	"mindonmap/models"
	"mindonmap/pkgs/e"
	"mindonmap/pkgs/setting"
	"mindonmap/pkgs/util"
	"mindonmap/validation"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/segmentio/ksuid"
)

type Mindflow struct {
	ID             int    `json:"-"`
	Mindid         string `json:"mindid"`
	Acid           int    `json:"acid"`
	Filename       string `json:"filename"`
	Mapdata        string `json:"mapdata"`
	Image          string `json:"image"`
	State          int    `json:"state"`
	Sharestate     int    `json:"sharestate"`
	Orders         int    `json:"orders"`
	Version        int    `json:"version"`
	CreatePlatform string `json:"create_platform"`
	SavePlatform   string `json:"Save_platform"`
}

// CopyMind 拷贝单个文件
func CopyFlow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}

	valid := validation.Validation{}
	valid.Required(json.Mindid, "mindid").Message("不能为空")

	// 权益检测
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	var total = MindMapTotal(Userid)
	pc := NewPrivilegeController()
	var isvip = IsVip(c)
	var CheckPriv bool
	if isvip {
		CheckPriv = pc.CheckPrivilege("vip", "cloud_storage_limit", total)
	} else {
		CheckPriv = pc.CheckPrivilege("free", "cloud_storage_limit", total)
	}

	if !CheckPriv {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_PERMISSION,
			"msg":  e.GetMsg(e.ERROR_AUTH_PERMISSION),
			"data": make(map[string]interface{}),
		})
		return
	}

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if models.ExistMindFlowByID(json.Mindid, Userid) {
			models.CopyFlow(json.Mindid)
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": "",
	})
}

// 新增流程图
func AddMindFlow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}

	var total = MindMapTotal(Userid)
	pc := NewPrivilegeController()
	var isvip = IsVip(c)

	var user_status = 0
	var CheckPriv bool
	if isvip {
		user_status = 1
		CheckPriv = pc.CheckPrivilege("vip", "cloud_storage_limit", total)
	} else {
		CheckPriv = pc.CheckPrivilege("free", "cloud_storage_limit", total)
	}
	if !CheckPriv {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_PERMISSION,
			"msg":  e.GetMsg(e.ERROR_AUTH_PERMISSION),
			"data": make(map[string]interface{}),
		})
		return
	}

	Mindid := ksuid.New().String()
	code := e.INVALID_PARAMS
	data := make(map[string]interface{})
	if !models.ExistMindFlowByMindID(Mindid) {

		image := util.WriteFile(strconv.Itoa(Userid), json.Mindid, json.Image)
		if len(image) > 0 {
			var aws3 = setting.GetAWSS3()
			S3Host := aws3.S3Host
			image = S3Host + image
		}

		dataBase := make(map[string]interface{})
		dataBase["acid"] = Userid
		dataBase["mapdata"] = json.Mapdata
		dataBase["modifietime"] = time.Now().Unix()
		dataBase["image"] = image
		dataBase["pid"] = Mindid
		models.AddMindFlowData(dataBase)

		// fmt.Println("json.CreatePlatform", json.CreatePlatform)
		data["mindid"] = Mindid
		data["acid"] = Userid
		data["filename"] = json.Filename
		data["mapdata"] = json.Mapdata
		data["image"] = image
		data["create_platform"] = json.CreatePlatform
		data["state"] = 0
		data["user_status"] = user_status
		res := models.AddMindFlow(data)

		code = e.SUCCESS
		if res {
			code = e.SUCCESS
		} else {
			data = nil
			code = e.ERROR_ADD
		}

	} else {
		data["mindid"] = Mindid
		data["filename"] = json.Filename
		code = e.ERROR_EXIST_PARAMS
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 保存脑图
func EditMindFlow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	valid := validation.Validation{}

	valid.Required(json.Mindid, "mindid").Message("不能为空")
	fmt.Println(json.Mindid)
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if models.ExistMindFlowByID(json.Mindid, Userid) {
			image := util.WriteFile(strconv.Itoa(Userid), json.Mindid, json.Image)
			if len(image) > 0 {
				var aws3 = setting.GetAWSS3()
				S3Host := aws3.S3Host
				image = S3Host + image
			}
			data := make(map[string]interface{})
			data["modifietime"] = time.Now().Unix()
			data["image"] = image
			models.EditMindFlow(json.Mindid, data)

			if models.ExistMindFlowByPID(json.Mindid) {
				data = make(map[string]interface{})
				data["acid"] = Userid
				data["mapdata"] = json.Mapdata
				data["modifietime"] = time.Now().Unix()
				data["image"] = image
				data["pid"] = json.Mindid
				data["save_platform"] = json.SavePlatform
				models.EditMindFlowData(json.Mindid, data)
				code = e.SUCCESS
			} else {
				data = make(map[string]interface{})
				data["acid"] = Userid
				data["mapdata"] = json.Mapdata
				data["modifietime"] = time.Now().Unix()
				data["image"] = image
				data["pid"] = json.Mindid
				models.AddMindFlowData(data)
				code = e.SUCCESS
			}

			code = e.SUCCESS
			datas := make(map[string]interface{})

			// 离开时则更新版本1
			if json.State == 0 {
				if !models.ExistMindVersionBySourceID(json.Mindid) {
					datas["pid"] = json.Mindid
					datas["acid"] = Userid
					datas["mapdata"] = json.Mapdata
					datas["state"] = 0
					datas["image"] = image
					datas["version"] = 1
					models.AddMindVersion(datas)
				} else {
					datas["pid"] = json.Mindid
					datas["acid"] = Userid
					datas["mapdata"] = json.Mapdata
					datas["image"] = image
					datas["modifietime"] = time.Now().Unix()
					datas["version"] = 1
					models.EditMindVersionSource(json.Mindid, datas)
				}
			}
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 自动保存脑图
func AutoSaveMindFlowData(c *gin.Context) {

	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	code := e.INVALID_PARAMS
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	if models.ExistMindFlowByID(json.Mindid, Userid) {
		image := util.WriteFile(strconv.Itoa(Userid), json.Mindid, json.Image)
		if len(image) > 0 {
			var aws3 = setting.GetAWSS3()
			S3Host := aws3.S3Host
			image = S3Host + image
		}
		data := make(map[string]interface{})
		data["acid"] = Userid
		data["modifietime"] = time.Now().Unix()
		data["image"] = image
		models.EditMindFlow(json.Mindid, data)
		code = e.SUCCESS
	}
	if models.ExistMindFlowByPID(json.Mindid) {
		image := util.WriteFile(strconv.Itoa(Userid), json.Mindid, json.Image)
		if len(image) > 0 {
			var aws3 = setting.GetAWSS3()
			S3Host := aws3.S3Host
			image = S3Host + image
		}
		data := make(map[string]interface{})
		data["acid"] = Userid
		data["mapdata"] = json.Mapdata
		data["modifietime"] = time.Now().Unix()
		data["image"] = image
		data["pid"] = json.Mindid
		models.EditMindFlowData(json.Mindid, data)
		code = e.SUCCESS
	} else {
		image := util.WriteFile(strconv.Itoa(Userid), json.Mindid, json.Image)
		if len(image) > 0 {
			var aws3 = setting.GetAWSS3()
			S3Host := aws3.S3Host
			image = S3Host + image
		}
		data := make(map[string]interface{})
		data["acid"] = Userid
		data["mapdata"] = json.Mapdata
		data["modifietime"] = time.Now().Unix()
		data["image"] = image
		data["pid"] = json.Mindid
		models.AddMindFlowData(data)
		code = e.SUCCESS
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 获取单个流程图
func GetMindFlow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	valid := validation.Validation{}
	valid.Required(json.Mindid, "mindid").Message("不能为空")
	var isvip = IsVip(c)
	if isvip {
		models.SaveActiveLog(Userid, 1, 2)
	} else {
		models.SaveActiveLog(Userid, 0, 2)
	}
	code := e.INVALID_PARAMS
	var data interface{}
	if !valid.HasErrors() {
		if models.ExistMindFlowByID(json.Mindid, Userid) {
			data = models.GetMindFlow(json.Mindid)
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 获取单个脑图数据
func GetMindFlowBase(c *gin.Context) {
	var json models.MindflowData
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}

	valid := validation.Validation{}
	valid.Required(json.Pid, "pid").Message("不能为空")

	code := e.INVALID_PARAMS
	var data interface{}
	if !valid.HasErrors() {

		minds := models.GetMindFlowData(json.Pid)

		if minds.ID > 0 {
			data = minds
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 获取多个已删除脑图
func GetMindFlowsDel(c *gin.Context) {
	data := make(map[string]interface{})

	valid := validation.Validation{}

	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.SUCCESS
		data["lists"] = models.GetMindFlowsDelList(util.GetPage(c), setting.PageSize, Userid)
		data["total"] = 0

	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 获取多个脑图
func GetMindFlows(c *gin.Context) {
	data := make(map[string]interface{})
	mapsdata := make(map[string]interface{})
	var maps Mindflow
	valid := validation.Validation{}

	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}

	if maps.State == 3 {
		mapsdata["state"] = maps.State
	} else {
		mapsdata["state"] = maps.State
		mapsdata["acid"] = Userid
	}
	var sorts string
	switch maps.Orders {
	case 1:
		sorts = "createtime desc"
	case 2:
		sorts = "createtime desc"
	case 3:
		sorts = "createtime desc"
	case 4:
		sorts = "createtime desc"
	default:
		sorts = "createtime desc"
	}
	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.SUCCESS
		if maps.State == 3 {
			data["lists"] = models.GetMindFlowsTempate(util.GetPage(c), setting.PageSize, mapsdata)
		} else {
			data["lists"] = models.GetMindFlowslist(util.GetPage(c), setting.PageSize, Userid, sorts)
		}
		println(data["lists"])
		data["total"] = models.GetMindFlowTotal(Userid)

	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 批量删除脑图
func DeleteMindFlows(c *gin.Context) {
	type MindFlowlist struct {
		Mindid []string
	}
	var json MindFlowlist
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	valid := validation.Validation{}
	valid.Required(json.Mindid, "mindid").Message("不能为空")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		data := make(map[string]interface{})
		data["state"] = 1
		data["statetime"] = time.Now().Unix()
		models.DeleteMindFlows(json.Mindid, Userid, data)
		code = e.SUCCESS
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 删除脑图
func DeleteMindFlow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	valid := validation.Validation{}
	valid.Required(json.Mindid, "mindid").Message("不能为空")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if models.ExistMindFlowByID(json.Mindid, Userid) {
			data := make(map[string]interface{})
			data["state"] = 1
			data["statetime"] = time.Now().Unix()
			models.DeleteMindFlow(json.Mindid, data)
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 回收站还原
func RestoreMindFlow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	valid := validation.Validation{}
	valid.Required(json.Mindid, "mindid").Message("不能为空")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if models.ExistMindFlowByID(json.Mindid, Userid) {
			data := make(map[string]interface{})
			data["state"] = 0
			data["statetime"] = time.Now().Unix()
			models.DeleteMindFlow(json.Mindid, data)
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 彻底移除回收站
func DelMindFlow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	valid := validation.Validation{}
	valid.Required(json.Mindid, "mindid").Message("不能为空")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if models.ExistMindFlowByID(json.Mindid, Userid) {
			data := make(map[string]interface{})
			data["state"] = 2
			data["statetime"] = time.Now().Unix()
			models.DeleteMindFlow(json.Mindid, data)
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 清空回收站
func CleanRecycleMindFlow(c *gin.Context) {

	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	valid := validation.Validation{}

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		data := make(map[string]interface{})
		data["state"] = 2
		data["statetime"] = time.Now().Unix()
		models.DeleteAllMindFlows(Userid, data)
		code = e.SUCCESS
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 脑图重命名
func RenameMindflow(c *gin.Context) {
	var json Mindflow
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	code := e.INVALID_PARAMS
	if models.ExistMindFlowByID(json.Mindid, Userid) {
		data := make(map[string]interface{})
		data["filename"] = json.Filename
		models.EditMindFlow(json.Mindid, data)
		code = e.SUCCESS
	} else {
		code = e.ERROR_NOT_EXIST_ARTICLE
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

// 新增脑图历史数据
func AddMindFlowVersion(c *gin.Context) {
	var json models.MindVersion

	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	//timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	if models.ExistMindFlowByPID(json.Pid) {
		data := make(map[string]interface{})
		data["acid"] = Userid
		data["mapdata"] = json.Mapdata
		if len(json.Mapdata) > 0 {
			models.EditMindFlowData(json.Pid, data)
		}
	}

	code := e.SUCCESS
	data := make(map[string]interface{})

	data["pid"] = json.Pid
	data["acid"] = Userid
	data["mapdata"] = json.Mapdata
	data["state"] = 0
	data["image"] = ""
	data["version"] = 2
	models.AddMindVersion(data)

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})

}

type MindImage struct {
	Acid   string `json:"acid"`
	Mindid string `json:"mindid"`
}

func UploadDocument(c *gin.Context) {

	mindid := c.PostForm("mindid")

	var strUserid string
	// 从Header中获取token
	authHeader := c.GetHeader("Authorization")
	bearerToken := strings.TrimPrefix(authHeader, "Bearer ")
	_, claims, err := parseToken(bearerToken)
	Userid := claims["userId"].(string)
	if err != nil {
		strUserid = "anonymous"
	} else {
		strUserid = Userid
	}

	var JsondataInfo = &MindImage{
		Acid:   strUserid,
		Mindid: mindid,
	}

	valid := validation.Validation{}
	valid.Required(JsondataInfo.Mindid, "mindid").Message("不能为空")
	valid.Required(JsondataInfo.Acid, "acid").Message("不能为空")
	code := e.SUCCESS
	data := make(map[string]interface{})
	if !valid.HasErrors() {

		fileHeader, _ := c.FormFile("file")
		f, _ := fileHeader.Open()
		var size int64 = fileHeader.Size
		buffer := make([]byte, size)
		f.Read(buffer)

		now := time.Now()
		filepath := now.Format("2006-01-02")
		Filename := filepath + "/" + JsondataInfo.Acid + "/" + JsondataInfo.Mindid + "/" + fileHeader.Filename

		data["url"] = util.WriteFileR2_image(Filename, size, buffer)

	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}
